package com.chuanyang.driverh.mainhome;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.responseresult.QualifyDetail;
import com.chuanyang.driverh.responseresult.QualifyDetailResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.TimeUtils;
import com.chuanyang.driverh.utils.eventbus.RefleshQualityEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;


public class NewQualifyActivity extends BaseTitleActivity {
    private ImageView iv_newqualify_image;
    private TextView tv_newqualify_name;
    private TextView tv_newqualify_phone;
    private TextView tv_newqualify_tradetype;
    private TextView tv_newqualify_startcity;
    private TextView tv_newqualify_endcity;
    private TextView tv_newqualify_goodname;
    private TextView tv_newqualify_goodweight;
    private TextView tv_newqualify_endtime;
    private TextView tv_newqualify_act;
    private EditText tv_newqualify_bidweight;
    private EditText tv_newqualify_bidprice;
    private TextView tv_newqualify_mark;
    private EditText et_newqualify_remark;

    private String advancedDriverId;
    private QualifyDetail data;
    private int type=1;


    @Override
    protected int getContentResId() {
        return R.layout.home_act_newqualify;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        showActivityLoading();
        iv_newqualify_image = (ImageView) findViewById(R.id.iv_newqualify_image);
        tv_newqualify_name = (TextView) findViewById(R.id.tv_newqualify_name);
        tv_newqualify_phone = (TextView) findViewById(R.id.tv_newqualify_phone);
        tv_newqualify_tradetype = (TextView) findViewById(R.id.tv_newqualify_tradetype);
        tv_newqualify_startcity = (TextView) findViewById(R.id.tv_newqualify_startcity);
        tv_newqualify_endcity = (TextView) findViewById(R.id.tv_newqualify_endcity);
        tv_newqualify_goodname = (TextView) findViewById(R.id.tv_newqualify_goodname);
        tv_newqualify_goodweight = (TextView) findViewById(R.id.tv_newqualify_goodweight);
        tv_newqualify_endtime = (TextView) findViewById(R.id.tv_newqualify_endtime);
        // tv_newqualify_goodprice = (TextView) findViewById(R.id.tv_newqualify_goodprice);
        tv_newqualify_mark = (TextView) findViewById(R.id.tv_newqualify_mark);
        tv_newqualify_act = (TextView) findViewById(R.id.tv_newqualify_act);
        tv_newqualify_bidweight = (EditText) findViewById(R.id.tv_newqualify_bidweight);
        tv_newqualify_bidprice = (EditText) findViewById(R.id.tv_newqualify_bidprice);
        et_newqualify_remark = (EditText) findViewById(R.id.et_newqualify_remark);


    }

    @Override
    protected void initData() {
        advancedDriverId = getIntent().getStringExtra("advancedDriverId");
        type = getIntent().getIntExtra("type", 0);
        setBackImg();
        if (type == 1) {
            getTitleTextView().setText("最新标单");
        } else {
            getTitleTextView().setText("修改标单");
        }

        if (type == 1) {
            tv_newqualify_act.setText("申报");
        } else {
            tv_newqualify_act.setText("修改");
        }

        getData(advancedDriverId);
    }

    @Override
    protected void initListener() {
        tv_newqualify_act.setOnClickListener(this);
    }



    private void fillView() {
        if (data==null)return;
        BitmapUtil.showImgForNetPath( data.getUserImage(),iv_newqualify_image);
        tv_newqualify_name.setText(data.getUsername());
        tv_newqualify_phone.setText(data.getPhoneCall());
        if (data.getCurrency() == 1) {
            tv_newqualify_tradetype.setVisibility(View.GONE);
            // tv_newqualify_goodprice.setText(data.getPrice() + "￥/t");

        } else {
            // tv_newqualify_goodprice.setText(data.getPrice() + "＄/t");
        }
        if (TextUtils.isEmpty(data.getRemark())) {
            tv_newqualify_mark.setText("---");
        } else {
            tv_newqualify_mark.setText(data.getRemark());
        }
        tv_newqualify_startcity.setText(data.getStartCity());
        tv_newqualify_endcity.setText(data.getTargetCity());
        tv_newqualify_goodname.setText(data.getGoodsName());
        tv_newqualify_goodweight.setText(data.getGoodsWeight() + "t");
        tv_newqualify_endtime.setText(data.getOrder_endtime());
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.tv_newqualify_act) {
            if (checkAll()) {
                updateAdvancedDriver();
            }
        }
    }

    private boolean checkAll() {
        if (TextUtils.isEmpty(tv_newqualify_bidweight.getText())) {
           PrintUtil.toast(activityContext,"请填写欲竞标单重量");
            return false;

        } else if (TextUtils.isEmpty(tv_newqualify_bidprice.getText())) {
            PrintUtil.toast(activityContext,"请填写欲竞标单价格");

            return false;
        } else if (Double.parseDouble(tv_newqualify_bidprice.getText().toString()) > data.getPrice()) {
            PrintUtil.toast(activityContext,"竞标单价格不能高于底价");
            return false;
        }
        return true;
    }


    private void getData(String advancedDriverId) {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("advancedDriverId", advancedDriverId);
        MyHttpUtil.selectAdvancedDriverById(activityContext,params, new MyCallback<QualifyDetailResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                PrintUtil.toast(activityContext, e.getMessage());
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(QualifyDetailResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                hideActivityLoading();
                data = result.getData();
                fillView();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    private void updateAdvancedDriver() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("id", advancedDriverId);
        params.put("goodsMaxWeight", tv_newqualify_bidweight.getText().toString().trim());
        params.put("price", tv_newqualify_bidprice.getText().toString().trim());
        params.put("state", "2");
        params.put("bidTime", TimeUtils.getCurrentStandardTime());
        if (!TextUtils.isEmpty(et_newqualify_remark.getText())) {
            params.put("remark", et_newqualify_remark.getText().toString().trim());

        }
        MyHttpUtil.updateAdvancedDriver(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
             e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    if (type == 1) {
                        PrintUtil.toast(activityContext,"竞标成功！");
                        EventBus.getDefault().post(new RefleshQualityEvent(1,type));
                    } else {
                        PrintUtil.toast(activityContext,"修改成功！");
                        EventBus.getDefault().post(new RefleshQualityEvent(1,type));
                    }
                    finish();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });



    }

}
